package com.laiketui.plugin.common.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.laiketui.core.common.LKTSnowflakeIdWorker;
import com.laiketui.core.common.SplitUtils;
import com.laiketui.core.lktconst.GloabConst;
import com.laiketui.core.utils.tool.DataUtils;
import com.laiketui.core.utils.tool.DateUtil;
import com.laiketui.core.utils.tool.PinyinUtils;
import com.laiketui.core.utils.tool.SerializePhpUtils;
import com.laiketui.plugin.common.domain.Page;
import com.laiketui.plugin.common.domain.base.*;
import com.laiketui.plugin.common.domain.base.goods.CartModel;
import com.laiketui.plugin.common.domain.base.goods.FreightModel;
import com.laiketui.plugin.common.domain.base.goods.ProductImgModel;
import com.laiketui.plugin.common.domain.base.presell.PreSellGoodsModel;
import com.laiketui.plugin.common.domain.base.user.UserAddress;
import com.laiketui.plugin.common.domain.exception.LaiKeAPIException;
import com.laiketui.plugin.common.domain.lktconst.DictionaryConst;
import com.laiketui.plugin.common.domain.lktconst.ErrorCode;
import com.laiketui.plugin.common.domain.vo.AddStockVo;
import com.laiketui.plugin.common.domain.vo.DicVo;
import com.laiketui.plugin.common.domain.vo.UploadMerchandiseVo;
import com.laiketui.plugin.common.domain.vo.freight.DefaultFreightVO;
import com.laiketui.plugin.common.exceotion.LaiKeApiWarnException;
import com.laiketui.plugin.common.interfaces.*;
import com.laiketui.plugin.common.mapper.*;
import com.laiketui.plugin.common.tool.ImgUploadUtils;
import com.laiketui.plugin.common.tool.data.DataCheckTool;
import com.laiketui.plugin.common.tool.str.StringUtils;
import com.laiketui.root.common.BuilderIDTool;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.*;

/**
 * 关于商品的公共类
 *
 * @author Trick
 * @date 2020/11/11 14:56
 */
@Service
public class PublicGoodsServiceImpl implements PublicGoodsService {

    private final Logger logger = LoggerFactory.getLogger(PublicGoodsServiceImpl.class);

    @Autowired
    private CustomerModelMapper customerModelMapper;

    @Autowired
    private ProLabelModelMapper proLabelModelMapper;

    @Autowired
    private PreSellGoodsMapper preSellGoodsMapper;

    @Override
    public Map<String, Object> addPage(int storeId, String adminName, int mchId, int type) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            //运费集
            List<Map<String, Object>> freightModelList = new ArrayList<>();
            //产品类型集
            List<Map<String, Object>> spTypeList = new ArrayList<>();
            //产品展示位
            List<Map<String, Object>> showAdrList = new ArrayList<>();
            //单位
            List<String> unitList = new ArrayList<>();
            //插件集
            Map<String, Object> pluginArrMap = new HashMap<>(16);


            LKTSnowflakeIdWorker snowflakeIdWorker = new LKTSnowflakeIdWorker();
            //获取运费
            FreightModel freightModel = new FreightModel();
            freightModel.setStore_id(storeId);
            freightModel.setMch_id(mchId);
            List<FreightModel> freightModels = freightModelMapper.select(freightModel);
            for (FreightModel freight : freightModels) {
                Map<String, Object> map = new HashMap<>(16);
                map.put("id", freight.getId());
                map.put("name", freight.getName());
                map.put("is_default", freight.getIs_default());
                freightModelList.add(map);
            }
            Map<String, Object> parmaMap = new HashMap<>(16);
            parmaMap.put("status", 1);
            //获取产品类型
            ProLabelModel proLabelModel = new ProLabelModel();
            proLabelModel.setStore_id(storeId);
            List<ProLabelModel> proLabelModelList = proLabelModelMapper.select(proLabelModel);
            for (ProLabelModel proLabel : proLabelModelList) {
                Map<String, Object> goodsTypeMap = new HashMap<>(3);
                goodsTypeMap.put("name", proLabel.getName());
                goodsTypeMap.put("value", proLabel.getId());
                goodsTypeMap.put("status", false);
                spTypeList.add(goodsTypeMap);
            }
            //获取商品展示位
            parmaMap.put("name", "商品展示位置");
            List<Map<String, Object>> showAdrListTemp = dictionaryListModelMapper.getDictionaryDynamic(parmaMap);
            for (Map<String, Object> map : showAdrListTemp) {
                Map<String, Object> goodsShowAdrMap = new HashMap<>(3);
                goodsShowAdrMap.put("name", map.get("text"));
                goodsShowAdrMap.put("value", map.get("value"));
                goodsShowAdrMap.put("status", false);
                showAdrList.add(goodsShowAdrMap);
            }

            //获取单位
            parmaMap.put("name", "单位");
            List<Map<String, Object>> unitMap = dictionaryListModelMapper.getDictionaryDynamic(parmaMap);
            for (Map<String, Object> map : unitMap) {
                unitList.add(String.valueOf(map.get("text")));
            }
            //如果是平台则获取插件
            List<Map<String, Object>> pluginArr = new ArrayList<>();
            if (type == GloabConst.LktConfig.LKT_CONFIG_TYPE_PT) {
                //获取插件集...暂时不做
                //$Plugin_arr = $Plugin->product_plugin($db, $store_id, 'product', '');
            } else {
                //获取所有商品支持的活动
                pluginArr = publiceService.getGoodsActive(storeId);
            }
            pluginArrMap.put("active", pluginArr);

            resultMap.put("product_number", snowflakeIdWorker.nextId());
            resultMap.put("plugin_list", pluginArrMap);
            resultMap.put("freight_list", freightModelList);
            resultMap.put("s_type", spTypeList);
            resultMap.put("show_adr", showAdrList);
            resultMap.put("unit", unitList);
        } catch (ClassCastException c) {
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSLXBZQ, "参数类型不正确", "addPage");
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("添加商品 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "addPage");
        }

        return resultMap;
    }

    @Override
    public Map<String, Object> editPage(int storeId, String adminName, int mchId, int goodsId, int type) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            //运费信息 全部
            List<Map<String, Object>> freightModelList;
            //商品运费信息
            Map<String, Object> goodsFreightMap = new HashMap<>(16);
            //商品图片信息(封面图集合)
            List<String> productShowImgList = new ArrayList<>();
            //商品品牌信息 全部
            List<Map<String, Object>> productBrandList;

            //商品分类
            List<ProductClassModel> productClassList = new ArrayList<>();
            //插件集信息 Plugin_arr
            Map<String, Object> pluginArr = new HashMap<>(16);

            ProductListModel productListModel = new ProductListModel();
            productListModel.setStore_id(storeId);
            productListModel.setId(goodsId);
            //如果是平台，则查询商品信息无视店铺
            if (type != GloabConst.LktConfig.LKT_CONFIG_TYPE_PT) {
                //否则加店铺id
                productListModel.setMch_id(mchId);
            }
            productListModel = productListModelMapper.selectOne(productListModel);
            if (productListModel != null) {
                //获取图片信息
                String imgUrl = productListModel.getImgurl();
                String converMapUrl = productListModel.getCover_map();
                imgUrl = publiceService.getImgPath(imgUrl, storeId);
                productListModel.setImgurl(imgUrl);
                converMapUrl = publiceService.getImgPath(converMapUrl, storeId);
                productListModel.setCover_map(converMapUrl);
            } else {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SPBCZ, "商品不存在");
            }
            //获取店铺运费信息
            FreightModel freightModel = new FreightModel();
            freightModel.setStore_id(storeId);
            freightModel.setMch_id(mchId);
            freightModelList = freightModelMapper.getFreightInfo(freightModel);
            for (Map<String, Object> map : freightModelList) {
                boolean selected = false;
                String fid = map.get("id").toString();
                if (fid.equals(productListModel.getFreight())) {
                    selected = true;
                }
                map.put("selected", selected);
            }
            //获取商品封面图
            boolean flag = false;
            com.laiketui.plugin.common.domain.base.goods.ProductImgModel productImgModel = new com.laiketui.plugin.common.domain.base.goods.ProductImgModel();
            productImgModel.setProduct_id(goodsId);
            List<Map<String, Object>> productImgModelList = productImgModelMapper.getProductImgInfoByPid(productImgModel);
            if (GloabConst.LktConfig.LKT_CONFIG_TYPE_PT == type || GloabConst.LktConfig.LKT_CONFIG_TYPE_PC == type) {
                flag = true;
            }
            if (productImgModelList != null) {
                for (int i = 0; i < productImgModelList.size(); i++) {
                    Map<String, Object> map = productImgModelList.get(i);
                    String productUrl = map.get("product_url").toString();
                    productUrl = publiceService.getImgPath(productUrl, storeId);
                    productShowImgList.add(productUrl);
                    if (flag) {
                        int isCenter = productUrl.equals(productImgModel.getProduct_url()) ? 1 : 0;
                        map.put("is_center", isCenter);
                    } else {
                        map.clear();
                        map.put(i + "", productUrl);
                    }
                }
            }
            //商品分类+品牌处理
            String res = productListModel.getProduct_class();
            String[] resList = res.split("-");
            //商品所属分类顶级
            int classIdTop = Integer.parseInt(resList[1]);
            int classNum = resList.length - 1;
            //商品所属分类
            int classId = Integer.parseInt(resList[classNum]);
            for (String id : resList) {
                if (com.laiketui.plugin.common.tool.str.StringUtils.isEmpty(id)) {
                    continue;
                }
                ProductClassModel productClass = new ProductClassModel();
                productClass.setCid(Integer.parseInt(id));
                productClass = productClassModelMapper.selectOne(productClass);
                productClassList.add(productClass);
            }
            //获取当前品牌名称
            String brandName = null;
            BrandClassModel brandClassModel = new BrandClassModel();
            brandClassModel.setBrand_id(productListModel.getBrand_id());
            brandClassModel = brandClassModelMapper.selectOne(brandClassModel);
            if (brandClassModel != null) {
                brandName = brandClassModel.getBrand_name();
            }
            //获取顶级牌信息
            Map<String, Object> parmaMap = new HashMap<>(16);
            parmaMap.put("store_id", storeId);
            parmaMap.put("status", 0);
            parmaMap.put("categories", classIdTop);
            productBrandList = brandClassModelMapper.getBrandClassDynamic(parmaMap);
            //商品主图
            Map<String, Object> defaultImgMap = new HashMap<>(16);
            defaultImgMap.put("product_url", productListModel.getImgurl());
            if (flag) {
                //品牌默认值
                Map<String, Object> defaultBrandMap = new HashMap<>(16);
                defaultBrandMap.put("brand_id", 0);
                defaultBrandMap.put("brand_name", "请选择品牌");
                if (productBrandList == null) {
                    productBrandList = new ArrayList<>();
                    productBrandList.add(defaultBrandMap);
                }
                defaultImgMap.put("is_center", 1);
            } else {
                //获取当前商品运费信息
                String fname = "";
                FreightModel goodsFreight = new FreightModel();
                goodsFreight.setId(Integer.parseInt(productListModel.getFreight()));
                goodsFreight = freightModelMapper.selectOne(goodsFreight);
                if (goodsFreight != null) {
                    fname = goodsFreight.getName();
                }
                goodsFreightMap.put("id", productListModel.getFreight());
                goodsFreightMap.put("name", fname);
            }
            //把商品主图放到图片列表中的第一个
            if (productImgModelList == null) {
                productImgModelList = new ArrayList<>();
                productImgModelList.add(defaultImgMap);
            } else {
                if (productImgModelList.size() < 1) {
                    productImgModelList.add(defaultImgMap);
                } else {
                    productImgModelList.set(0, defaultImgMap);
                }
            }

            //规格值集
            List<Map<String, Object>> strArrList = new ArrayList<>();
            List<Map<String, Object>> checkedAttrList = new ArrayList<>();
            List<Map<String, Object>> attrGroupList = new ArrayList<>();

            ConfiGureModel confiGureModel = new ConfiGureModel();
            confiGureModel.setPid(goodsId);
            confiGureModel.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS.toString());
            List<ConfiGureModel> confiGureModelList = confiGureModelMapper.select(confiGureModel);
            if (confiGureModelList != null && confiGureModelList.size() > 0) {
                //规格处理
                String attribute = confiGureModelList.get(0).getAttribute();
                if (!StringUtils.isEmpty(attribute)) {
                    Map<String, Object> attributeMap = DataUtils.cast(SerializePhpUtils.getUnserializeObj(attribute, Map.class));
                    if (attributeMap != null) {
                        for (String key : attributeMap.keySet()) {
                            Map<String, Object> dataMap = new HashMap<>(16);
                            String attribyteKey = key;

                            int index = attribyteKey.indexOf("_LKT_");
                            if (index > 0) {
                                //属性名称
                                attribyteKey = attribyteKey.substring(0, attribyteKey.indexOf("_LKT_"));
                            }
                            dataMap.put("attr_group_name", attribyteKey);
                            attrGroupList.add(dataMap);
                        }
                    }
                    //属性名称集合,去重
                    List<String> tempList = new ArrayList<>();
                    for (ConfiGureModel confiGure : confiGureModelList) {
                        //当前属性信息
                        List<Map<String, Object>> attrLists = new ArrayList<>();
                        String attributeStr = confiGure.getAttribute();
                        Map<String, Object> attributeStrMap = DataUtils.cast(SerializePhpUtils.getUnserializeObj(attributeStr, Map.class));
                        if (attributeStrMap != null) {
                            for (String key : attributeStrMap.keySet()) {
                                String attributeKey = key;
                                String attribyteValue = attributeStrMap.get(key).toString();
                                int index = attributeKey.indexOf("_LKT_");
                                if (index > 0) {
                                    //获取id (尺码_LKT_8) 左边为名称 最后一个是id
                                    int keyId = attributeKey.lastIndexOf("-") + 1;
                                    int valueId = attribyteValue.indexOf("_") + 1;
                                    String keyIdTemp = attributeKey.substring(keyId);
                                    String valueIdTemp = attribyteValue.substring(0, valueId);
                                    Map<String, Object> dataMap = new HashMap<>(16);
                                    dataMap.put("id0", keyIdTemp);
                                    dataMap.put("id1", valueIdTemp);
                                    strArrList.add(dataMap);
                                    //获取名称
                                    attributeKey = attributeKey.substring(0, attributeKey.indexOf("_LKT"));
                                    attribyteValue = attribyteValue.substring(0, attribyteValue.indexOf("_LKT"));
                                }
                                for (Map<String, Object> map : attrGroupList) {
                                    //规格集 (attr_name:value,status:false)
                                    List<Map<String, Object>> attrList = new ArrayList<>();
                                    if (map.containsKey("attr_list")) {
                                        attrList = DataUtils.cast(map.get("attr_list"));
                                    }
                                    if (attributeKey.equals(map.get("attr_group_name").toString())) {
                                        //判断当前规格值是否存在数组中，不存在则添加
                                        if (!tempList.contains(attribyteValue)) {
                                            Map<String, Object> attrListMap = new HashMap<>(16);
                                            boolean isdsj = false;
                                            attrListMap.put("attr_name", attribyteValue);
                                            //商品是否待上架
                                            if (DictionaryConst.GoodsStatus.NOT_GROUNDING.toString().equals(productListModel.getStatus())) {
                                                isdsj = true;
                                            }
                                            attrListMap.put("status", isdsj);
                                            attrList.add(attrListMap);
                                            map.put("attr_list", attrList);
                                            tempList.add(attribyteValue);
                                        }
                                    }
                                }
                                Map<String, Object> attrListsMap = new HashMap<>(16);
                                attrListsMap.put("attr_id", "");
                                attrListsMap.put("attr_group_name", attributeKey);
                                attrListsMap.put("attr_name", attribyteValue);
                                attrLists.add(attrListsMap);
                            }
                        }

                        Map<String, Object> checkedAttrListMap = new HashMap<>(16);
                        checkedAttrListMap.put("attr_list", attrLists);
                        checkedAttrListMap.put("cbj", confiGure.getCostprice());
                        checkedAttrListMap.put("yj", confiGure.getYprice());
                        checkedAttrListMap.put("sj", confiGure.getPrice());
                        checkedAttrListMap.put("kucun", confiGure.getNum());
                        checkedAttrListMap.put("unit", confiGure.getUnit());
                        checkedAttrListMap.put("bar_code", confiGure.getBar_code());
                        checkedAttrListMap.put("name", confiGure.getName());
                        checkedAttrListMap.put("img", publiceService.getImgPath(confiGure.getImg(), storeId));
                        checkedAttrListMap.put("cid", confiGure.getId());
                        checkedAttrList.add(checkedAttrListMap);
                    }
                }
            }

            //产品属性处理
            List<Map<String, Object>> sTypeList = new ArrayList<>();
            String sType = productListModel.getS_type();
            List<String> arr = new ArrayList<>();
            if (StringUtils.isNotEmpty(sType)) {
                arr = Arrays.asList(sType.split(","));
            }
            //展示位置
            List<Map<String, Object>> showAdrList;
            String showAdr = productListModel.getShow_adr();
            List<String> showAdrs = new ArrayList<>();
            if (!StringUtils.isEmpty(showAdr)) {
                showAdr = StringUtils.trim(showAdr, SplitUtils.DH);
                showAdrs = Arrays.asList(showAdr.split(","));
            }

            //获取商品展示位
            parmaMap.clear();
            parmaMap.put("status", 1);
            parmaMap.put("name", "商品展示位置");
            showAdrList = dictionaryListModelMapper.getDictionaryDynamic(parmaMap);
            for (Map<String, Object> map : showAdrList) {
                boolean status = false;
                String value = map.get("value").toString();
                String text = map.get("text").toString();
                map.clear();
                map.put("name", text);
                map.put("value", value);
                if (showAdrs.contains(value)) {
                    status = true;
                }
                map.put("status", status);
            }
            //获取商品类型
            ProLabelModel proLabelModel = new ProLabelModel();
            proLabelModel.setStore_id(storeId);
            List<ProLabelModel> proLabelModelList = proLabelModelMapper.select(proLabelModel);
            for (ProLabelModel proLabel : proLabelModelList) {
                Map<String, Object> spTypeMap = new HashMap<>(16);
                boolean status = false;
                spTypeMap.put("name", proLabel.getName());
                spTypeMap.put("value", proLabel.getId());
                if (arr.contains(proLabel.getId().toString())) {
                    status = true;
                }
                spTypeMap.put("status", status);
                sTypeList.add(spTypeMap);
            }
            //查询商品其它信息
            Map<String, Object> distributorsMap = new HashMap<>(16);
            if (GloabConst.LktConfig.LKT_CONFIG_TYPE_PT == type) {
                //获取插件 暂时不做...
//                $Plugin_arr = $Plugin->product_plugin($db, $store_id, 'product', $active, $distributor_id);
            } else {
                //获取商品支持的活动类型
                List<Map<String, Object>> activeMainList = publiceService.getGoodsActive(storeId, Integer.parseInt(productListModel.getActive()));
                pluginArr.put("active", activeMainList);
                //获取商品分销等级
                String dengjiName = "会员专区商品绑定等级";
                Integer fenxiaoId = productListModel.getDistributor_id();
                if (fenxiaoId != null && fenxiaoId != 0) {
                    DistributionGradeModel distributionGradeModel = new DistributionGradeModel();
                    distributionGradeModel.setStore_id(storeId);
                    distributionGradeModel.setIs_ordinary(0);
                    distributionGradeModel.setId(fenxiaoId);
                    distributionGradeModel = distributionGradeModelMapper.selectOne(distributionGradeModel);
                    if (distributionGradeModel != null) {
                        Map<String, Object> setsMap = SerializePhpUtils.getDistributionGradeBySets(distributionGradeModel.getSets());
                        dengjiName = setsMap.get("s_dengjiname").toString();
                    }
                }
                distributorsMap.put("id", fenxiaoId == null ? 0 : fenxiaoId);
                distributorsMap.put("name", dengjiName);
            }
            //获取商品初始值
            String initialStr = productListModel.getInitial();
            Map<String, String> initialMap = DataUtils.cast(SerializePhpUtils.getUnserializeObj(initialStr, Map.class));
            if (initialMap != null) {
                if (!initialMap.containsKey("stockWarn")) {
                    initialMap.put("stockWarn", productListModel.getMin_inventory().toString());
                }
            }
            //单位处理
            List<String> units = new ArrayList<>(16);
            List<Map<String, Object>> unitMapList = new ArrayList<>();
            parmaMap.clear();
            parmaMap.put("status", 1);
            parmaMap.put("name", "单位");
            List<Map<String, Object>> unitList = dictionaryListModelMapper.getDictionaryDynamic(parmaMap);
            for (Map<String, Object> map : unitList) {
                String unit = map.get("text").toString();
                if (GloabConst.LktConfig.LKT_CONFIG_TYPE_PC == type) {
                    boolean checked = false;
                    Map<String, Object> dataMap = new HashMap<>(16);
                    dataMap.put("name", unit);
                    if (initialMap != null) {
                        if (initialMap.get("unit").equals(unit)) {
                            checked = true;
                        }
                    }
                    dataMap.put("checked", checked);
                    unitMapList.add(dataMap);
                } else {
                    units.add(unit);
                }
            }
            //预售商品信息
            PreSellGoodsModel preSellGoodsModel = new PreSellGoodsModel();
            preSellGoodsModel.setProduct_id(goodsId);
            preSellGoodsModel = preSellGoodsMapper.selectOne(preSellGoodsModel);
            if (!Objects.isNull(preSellGoodsModel)) {
                Map<String, Object> sellGoodMap = new HashMap<>();
                sellGoodMap.put("sell_type", preSellGoodsModel.getSell_type());
                if (preSellGoodsModel.getDeposit() != null) {
                    sellGoodMap.put("deposit", preSellGoodsModel.getDeposit());
                }
                if (preSellGoodsModel.getPay_type() != null) {
                    sellGoodMap.put("payType", preSellGoodsModel.getPay_type());
                }
                if (preSellGoodsModel.getDeposit_start_time() != null) {
                    sellGoodMap.put("depositStartTime", DateUtil.dateFormate(preSellGoodsModel.getDeposit_start_time(), GloabConst.TimePattern.YMDHMS));
                }
                if (preSellGoodsModel.getDeposit_end_time() != null) {
                    sellGoodMap.put("depositEndTime", DateUtil.dateFormate(preSellGoodsModel.getDeposit_end_time(), GloabConst.TimePattern.YMDHMS));
                }
                if (preSellGoodsModel.getBalance_pay_time() != null) {
                    sellGoodMap.put("balancePayTime", DateUtil.dateFormate(preSellGoodsModel.getBalance_pay_time(), GloabConst.TimePattern.YMD));
                }
                if (preSellGoodsModel.getSell_num() != null) {
                    sellGoodMap.put("sellNum", preSellGoodsModel.getSell_num());
                }
                if (preSellGoodsModel.getSurplus_num() != null){
                    sellGoodMap.put("surplusNum", preSellGoodsModel.getSurplus_num());
                }
                if (preSellGoodsModel.getEnd_day() != null) {
                    sellGoodMap.put("endDay", preSellGoodsModel.getEnd_day());
                }
                sellGoodMap.put("deliveryTime", preSellGoodsModel.getDelivery_time());
                resultMap.put("sellGoodInfo", sellGoodMap);
            }
            resultMap.put("mch_id", mchId);
            resultMap.put("list", productListModel);
            if (StringUtils.isNotEmpty(productListModel.getVideo())){
                resultMap.put("video", publiceService.getImgPath(productListModel.getVideo(), storeId));
            }
            if (StringUtils.isNotEmpty(productListModel.getProVideo())){
                resultMap.put("proVideo", publiceService.getImgPath(productListModel.getProVideo(), storeId));
            }
            resultMap.put("product_title", productListModel.getProduct_title());
            resultMap.put("subtitle", productListModel.getSubtitle());
            resultMap.put("keyword", productListModel.getKeyword());
            resultMap.put("weight", productListModel.getWeight());
            resultMap.put("product_number", productListModel.getProduct_number());
            resultMap.put("class_id", classId);
            resultMap.put("ctypes", productClassList);
            resultMap.put("brand_class", productBrandList);
            resultMap.put("brand_id", productListModel.getBrand_id());
            resultMap.put("imgurls", productShowImgList);
            resultMap.put("initial", initialMap);
            resultMap.put("status", productListModel.getStatus());
            if (units.size() > 0) {
                resultMap.put("unit", units);
            } else {
                resultMap.put("unit", unitMapList);
            }
            resultMap.put("attr_group_list", attrGroupList);
            resultMap.put("checked_attr_list", checkedAttrList);
            resultMap.put("min_inventory", productListModel.getMin_inventory());
            resultMap.put("freight_list", freightModelList);
            resultMap.put("sp_type", sTypeList);
            resultMap.put("active", productListModel.getActive());
            resultMap.put("Plugin_arr", pluginArr);
            resultMap.put("show_adr", showAdrList);
            resultMap.put("content", productListModel.getContent());
            resultMap.put("brand_name", brandName);
            resultMap.put("brand_class_list1", brandClassModel);
            resultMap.put("freight_list1", goodsFreightMap);
            resultMap.put("distributors", null);
            resultMap.put("distributors1", distributorsMap);
            resultMap.put("status", productListModel.getStatus());
            resultMap.put("richList", productListModel.getRichList());
            resultMap.put("strArr", strArrList);
            resultMap.put("cover_map", productListModel.getCover_map());
        } catch (ClassCastException c) {
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SJCW, "数据错误");
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("获取商品明细数据 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "editPage");
        }

        return resultMap;
    }


    @Override
    public Map<String, Object> productList(int storeId, String adminName, int mchId, int type, Map<String, Object> map) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            //结果集
            List<Map<String, Object>> list = new ArrayList<>();

            int pageNo = Integer.parseInt(map.get("page").toString());
            int pageSize = Integer.parseInt(map.get("pagesize").toString());
            Page page = Page.newBuilder(pageNo, pageSize, null);
            String pageTo = "";
            if (map.containsKey("pageto")) {
                pageTo = map.get("pageto").toString();
            }
            //分类 当前级别id+上级id : 当前级别名称
            Map<String, Object> productClassMap = new HashMap<>(16);
            //品牌
            Map<Integer, Object> brandClassMap = new HashMap<>(16);

            //分类集
            List<Map<String, Object>> productClassList = new ArrayList<>();
            //品牌集
            List<Map<String, Object>> brandList = new ArrayList<>();

            //获取分类下拉
            ProductClassModel productClassModel = new ProductClassModel();
            productClassModel.setStore_id(storeId);
            productClassModel.setSid(0);
            //获取所有分类 数据结构 -3-31-191-
            List<ProductClassModel> productClassModelAll = productClassModelMapper.getProductClassLevel(productClassModel);
            for (ProductClassModel productClassAll : productClassModelAll) {
                String coneCid = "-" + productClassAll.getCid() + "-";
                productClassMap.put(coneCid, productClassAll.getPname());
                //获取第一级
                productClassModel.setSid(productClassAll.getCid());
                List<ProductClassModel> productClassModeLeve1 = productClassModelMapper.getProductClassLevel(productClassModel);
                for (ProductClassModel productClassLeve1 : productClassModeLeve1) {
                    coneCid += productClassAll.getCid() + "-";
                    productClassMap.put(coneCid, productClassLeve1.getPname());
                    //获取第二级
                    productClassModel.setSid(productClassLeve1.getCid());
                    List<ProductClassModel> productClassModeLeve2 = productClassModelMapper.getProductClassLevel(productClassModel);
                    for (ProductClassModel productClass2 : productClassModeLeve2) {
                        coneCid += productClass2.getCid() + "-";
                        productClassMap.put(coneCid, productClass2.getPname());
                    }
                }
            }
            //获取品牌下拉
            BrandClassModel brandClassModel = new BrandClassModel();
            brandClassModel.setStore_id(storeId);
            brandClassModel.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS);
            List<BrandClassModel> brandClassModelList = brandClassModelMapper.select(brandClassModel);
            for (BrandClassModel brandClass : brandClassModelList) {
                brandClassMap.put(brandClass.getBrand_id(), brandClass.getBrand_name());
            }

            //sql参数列表 导出全部
            Map<String, Object> parmaMap1 = new HashMap<>(16);
            //sql参数列表
            Map<String, Object> parmaMap = new HashMap<>(16);
            parmaMap.put("store_id", storeId);
            if (type == GloabConst.LktConfig.LKT_CONFIG_TYPE_PT) {
                //1、先按照状态排序，优先显示待上架商品，待上架商品则按照发布时间排序；2、已上架和已下架商品则按照上架时间排序
                parmaMap.put("diy_sort", DataUtils.Sort.DESC.toString());
                if (map.containsKey("mchStatus")) {
                    int mchStatus = Integer.parseInt(map.get("mchStatus").toString());
                    if (mchStatus != 0) {
                        parmaMap.put("mch_status", mchStatus);
                    }
                }
            } else {
                if (type == GloabConst.LktConfig.LKT_CONFIG_TYPE_PC) {
                    parmaMap.put("diy_sort", DataUtils.Sort.DESC.toString());
                } else {
                    parmaMap.put("sort_sort", DataUtils.Sort.DESC.toString());
                }
                String mchStatusTemp = map.get("mch_status") + "";
                if (!StringUtils.isEmpty(mchStatusTemp)) {
                    parmaMap.put("mch_id", mchId);
                    //商品类型 虚拟/实物
                    if (map.containsKey("commodity_type")) {
                        parmaMap.put("commodity_type", map.get("commodity_type"));
                    }
                    int mchStatus = Integer.parseInt(mchStatusTemp);
                    if (mchStatus == 1) {
                        //商品列表只显示已审核通过的商品
                        parmaMap.put("mch_status", DictionaryConst.GoodsMchExameStatus.EXAME_PASS_STATUS);
                    } else if (mchStatus == 2) {
                        //mch_status != 2
                        parmaMap.put("mch_status", -2);
                    } else if (mchStatus == 3) {
                        parmaMap.put("mch_status", DictionaryConst.GoodsMchExameStatus.EXAME_NOT_PASS_STATUS);
                    } else if (mchStatus == 4) {
                        parmaMap.put("mch_status", DictionaryConst.GoodsMchExameStatus.EXAME_STOP_STATUS);
                    } else if (mchStatus == 5) {
                        //待审核的
                        parmaMap.put("mch_status", DictionaryConst.GoodsMchExameStatus.EXAME_WAIT_STATUS);
                    }
                }
                //自选商品流程
                if (map.containsKey("isZx")) {
                    boolean isZx = MapUtils.getBooleanValue(map, "isZx");
                    if (isZx) {
                        int myMchId = MapUtils.getInteger(map, "myMchId");
                        parmaMap.put("is_zixuan", 1);
                        parmaMap.put("checkZx_mchId", myMchId);
                    }
                }
            }
            //剔除自选商品 add by trick 2021-12-17 11:35:41
            if (map.containsKey("notZx") && MapUtils.getBooleanValue(map, "notZx")) {
                int myMchId = MapUtils.getInteger(map, "myMchId");
                parmaMap.put("not_checkZx_mchId", myMchId);
            }
            parmaMap1.putAll(parmaMap);

            //是否需要查询商品分类
            if (map.containsKey("product_class")) {
                Map<String, Object> brandMap = new HashMap<>(16);
                brandMap.put("brand_id", "0");
                brandMap.put("brand_name", "请选择品牌");

                int classId = Integer.parseInt(map.get("product_class").toString());
                if (classId > 0) {
                    //商品分类递归找上级
                    String classIdListStr = strOption(storeId, classId, "");
                    String[] classIds = StringUtils.trim(classIdListStr, "-").split("-");
                    //查询子分类要带出上级,查上级不能带出下级数据
                    parmaMap.put("product_class", classIds[classIds.length - 1]);
                    //商品分类顶级
                    int classIdTop = Integer.parseInt(classIds[0]);
                    for (String cid : classIds) {
                        int id = Integer.parseInt(cid);
                        ProductClassModel product = new ProductClassModel();
                        product.setStore_id(storeId);
                        product.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS);
                        product.setCid(id);
                        product = productClassModelMapper.selectOne(product);
                        if (product != null) {
                            productClassList.add(brandMap);
                        }
                    }
                    //获取顶级类别品牌
                    Map<String, Object> parmaBrandMap = new HashMap<>(16);
                    parmaBrandMap.put("store_id", storeId);
                    parmaBrandMap.put("status", 0);
                    parmaBrandMap.put("categories", classIdTop);
                    brandList = brandClassModelMapper.getBrandClassDynamic(parmaBrandMap);
                }
                brandList.add(0, brandMap);
            }
            //商品Id
            if (map.containsKey("goodsId")) {
                Integer goodsId = MapUtils.getInteger(map, "goodsId");
                parmaMap.put("goodsId", goodsId);
            }
            //是否需要查询品牌id
            if (map.containsKey("brand_id")) {
                int brandIdTemp = Integer.parseInt(map.get("brand_id").toString());
                if (brandIdTemp != 0) {
                    parmaMap.put("brand_id", brandIdTemp);
                }
            }
            //是否需要查询商品上架状态
            if (map.containsKey("status")) {
                int statusTemp = Integer.parseInt(map.get("status").toString());
                if (statusTemp != 0) {
                    parmaMap.put("goodsStatus", statusTemp);
                }
            } else if (map.containsKey("statusList")) {
                List<Integer> statusList = DataUtils.cast(map.get("statusList"));
                if (statusList != null) {
                    parmaMap.put("statusList", statusList);
                }
            }
            //是否需要查询商品互动状态
            if (map.containsKey("active")) {
                int activeTemp = Integer.parseInt(map.get("active").toString());
                if (activeTemp != 0) {
                    parmaMap.put("active", activeTemp);
                }
            }
            //是否需要查询商品标题
            if (map.containsKey("product_title")) {
                String productTitle = map.get("product_title") + "";
                if (!StringUtils.isEmpty(productTitle)) {
                    if (productTitle.indexOf(" ") > 0) {
                        //多个标题搜索
                        List<String> productTitleList = Arrays.asList(productTitle.split(" "));
                        parmaMap.put("productTitleList", productTitleList);
                    } else {
                        parmaMap.put("product_title", productTitle);
                    }
                }
            }
            //是否需要查询店铺名称
            if (map.containsKey("mch_name")) {
                String mchName = map.get("mch_name") + "";
                if (StringUtils.isNotEmpty(mchName)) {
                    parmaMap.put("mch_name", mchName);
                }
            }
            if (map.containsKey("mchNameOrGoodsName")) {
                String key = map.get("mchNameOrGoodsName") + "";
                if (StringUtils.isNotEmpty(key)) {
                    parmaMap.put("mchNameOrGoodsName", key);
                }
            }
            //是否需要查询展示位置
            if (map.containsKey("show_adr")) {
                String showAdr = map.get("show_adr") + "";
                if (StringUtils.isNotEmpty(showAdr)) {
                    parmaMap.put("show_adr", showAdr);
                }
            }
            //是否需要查询商品标签
            if (map.containsKey("goodsTga")) {
                String showAdr = map.get("goodsTga") + "";
                if (StringUtils.isNotEmpty(showAdr)) {
                    parmaMap.put("s_type", showAdr);
                }
            }

            boolean isAll = false;
            if ("whole".equals(pageTo)) {
                //导出全部
                isAll = true;
            } else {
                //导出查询无需分页
                if (!"inquiry".equals(pageTo)) {
                    //导出本页 This_page
                    parmaMap.put("page", page.getPageNo());
                    parmaMap.put("pageSize", page.getPageSize());
                }
            }
            //根据条件统计
            int total = productListModelMapper.getProductListLeftJoinMchCountDynamic(parmaMap);
            List<Map<String, Object>> productList;
            if (isAll) {
                //导出查询
                productList = productListModelMapper.getProductListLeftJoinMchDynamic(parmaMap1);
            } else {
                productList = productListModelMapper.getProductListLeftJoinMchDynamic(parmaMap);
            }
            if (productList != null) {
                for (int i = 0; i < productList.size(); i++) {
                    Map<String, Object> goodMap = productList.get(i);
                    int goodsId = Integer.parseInt(goodMap.get("id").toString());
                    String classIds = goodMap.get("product_class").toString();
                    int brandId = Integer.parseInt(goodMap.get("brand_id").toString());
                    String initialStr = goodMap.get("initial").toString();
                    Map<String, String> initialMap = DataUtils.cast(SerializePhpUtils.getUnserializeObj(initialStr, Map.class));
                    if (initialMap == null) {
                        throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SJCW, "数据错误");
                    }
                    BigDecimal presentPrice = new BigDecimal(initialMap.get("sj"));
                    String unit = initialMap.get("unit");

                    //是否有未完成的订单
                    Integer rew = chaxun(goodsId, storeId);
                    goodMap.put("rew", 0);
                    if (rew != null) {
                        if (rew == 2) {
                            goodMap.put("rew", 2);
                        } else if (rew == 3) {
                            goodMap.put("rew", 3);
                        }
                    }
                    //获取上一条数据的id
                    Integer supperGoodsId = null;
                    boolean supperStatus = false;
                    if (i == 0) {
                        //第一页没有上一条
                        if (page.getPageNo() > 1) {
                            //获取上一页最后一条数据
                            Map<String, Object> parmaTempMap = new HashMap<>(16);
                            parmaTempMap.putAll(parmaMap);
                            parmaTempMap.put("page", page.getPageNo() - 1);
                            parmaTempMap.put("pageSize", 1);
                            //获取商品信息
                            List<Map<String, Object>> goodsFirstList = productListModelMapper.getProductListLeftJoinMchDynamic(parmaTempMap);
                            Map<String, Object> goodsFirstMap = goodsFirstList.get(0);
                            supperGoodsId = Integer.parseInt(goodsFirstMap.get("id").toString());
                            supperStatus = true;
                        }
                    } else {
                        //上一条数据id
                        supperGoodsId = Integer.parseInt(productList.get(i - 1).get("id").toString());
                        supperStatus = true;
                    }
                    goodMap.put("upper_status", supperStatus);
                    //获取下一条数据
                    Integer underneathGoodsId = null;
                    if (i == productList.size() - 1) {
                        Map<String, Object> parmaTempMap = new HashMap<>(16);
                        parmaTempMap.putAll(parmaMap);
                        parmaTempMap.put("pageSize", 1);
                        //当为最后一条数据
                        if (pageNo == 1) {
                            parmaTempMap.put("page", page.getPageNo());
                        } else {
                            int start = page.getPageNo() + page.getPageSize();
                            parmaTempMap.put("page", start);
                        }
                        //获取商品信息
                        List<Map<String, Object>> goodsFirstList = productListModelMapper.getProductListLeftJoinMchDynamic(parmaTempMap);
                        if (goodsFirstList.size() > 0) {
                            Map<String, Object> goodsFirstMap = goodsFirstList.get(0);
                            underneathGoodsId = Integer.parseInt(goodsFirstMap.get("id").toString());
                        }
                    } else {
                        underneathGoodsId = Integer.parseInt(productList.get(i + 1).get("id").toString());
                    }
                    goodMap.put("upper_id", supperGoodsId);
                    goodMap.put("underneath_id", underneathGoodsId);
                    String stype = goodMap.get("s_type") + "";
                    if (StringUtils.isNotEmpty(stype)) {
                        String[] stypeList = stype.split(",");
                        goodMap.put("s_type", stypeList);
                    }
                    //展示位置处理
                    String showAdr = goodMap.get("show_adr") + "";
                    List<String> showAdrNameList = new ArrayList<>();
                    if (StringUtils.isNotEmpty(showAdr)) {
                        showAdr = StringUtils.trim(showAdr, SplitUtils.DH);
                        String[] showAdrList = showAdr.split(",");
                        goodMap.put("showAdrList", showAdrList);
                        DicVo dicVo = new DicVo();
                        dicVo.setName("商品展示位置");
                        dicVo.setShowChild(true);
                        dicVo.setShowChild(true);
                        for (String showAdrId : showAdrList) {
                            dicVo.setValue(showAdrId);
                            Map<String, Object> showAdrMap = publicDictionaryService.getDictionaryByName(dicVo);
                            List<DictionaryListModel> showList = DataUtils.cast(showAdrMap.get("value"));
                            if (showList != null) {
                                for (DictionaryListModel dic : showList) {
                                    showAdrNameList.add(dic.getValue());
                                }
                            }
                        }
                    }
                    if (showAdrNameList.size() < 1) {
                        showAdrNameList.add("全部商品");
                    }
                    goodMap.put("showAdrNameList", showAdrNameList);
                    //总平台的商品分类名称
                    String commodityClassification = "";
                    //总平台的商品品牌名称
                    String commodityBrand = "";
                    //店主分类
                    String shopkeepersClassification = "";
                    if (StringUtils.isNotEmpty(classIds)) {
                        //-394-395-396- 转换成数组需要 -1
                        String[] classIdList = StringUtils.trim(classIds, "-").split("-");
                        int classId = Integer.parseInt(classIdList[classIdList.length - 1]);
                        ProductClassModel p = new ProductClassModel();
                        p.setStore_id(storeId);
                        p.setCid(classId);
                        p = productClassModelMapper.selectOne(p);
                        if (p != null) {
                            commodityClassification = p.getPname();
                        }
                        BrandClassModel b = new BrandClassModel();
                        b.setStore_id(storeId);
                        b.setBrand_id(brandId);
                        b = brandClassModelMapper.selectOne(b);
                        if (b != null) {
                            commodityBrand = b.getBrand_name();
                        }
                        goodMap.put("pname", commodityClassification);
                        goodMap.put("brand_name", commodityBrand);
                    }

                    //查询库存信息
                    ConfiGureModel confiGureModel = new ConfiGureModel();
                    Integer goodStockNum = confiGureModelMapper.countConfigGureNum(goodsId);
                    goodMap.put("num", goodStockNum);
                    //商品图片
                    String cgImg = "";
                    //获取价格
                    confiGureModel = confiGureModelMapper.getProductMinPriceAndMaxYprice(goodsId);
                    if (confiGureModel != null) {
                        unit = confiGureModel.getUnit();
                        presentPrice = confiGureModel.getPrice();
                    }

                    //获取商品主图
                    goodMap.put("imgurl", publiceService.getImgPath(productImgModelMapper.getProductImg(goodsId), storeId));
                    goodMap.put("unit", unit);
                    goodMap.put("price", presentPrice);
                    String statusName = "";
                    int status = Integer.parseInt(goodMap.get("status").toString());
                    if (DictionaryConst.GoodsStatus.NOT_GROUNDING == status) {
                        statusName = "待上架";
                    } else if (DictionaryConst.GoodsStatus.NEW_GROUNDING == status) {
                        statusName = "上架";
                    } else if (DictionaryConst.GoodsStatus.OFFLINE_GROUNDING == status) {
                        statusName = "下架";
                    }
                    goodMap.put("status_name", statusName);
                    goodMap.put("upper_shelf_time", goodMap.get("upper_shelf_time"));
                    Integer volume = MapUtils.getInteger(goodMap, "volume");
                    if (volume < 0) {
                        goodMap.put("volume", 0);
                    }
                    list.add(goodMap);
                }
            }


            resultMap.put("list", list);
            resultMap.put("total", total);
        } catch (ClassCastException c) {
            c.printStackTrace();
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSLXBZQ, "参数类型不正确", "productList");
        } catch (LaiKeAPIException l) {
            logger.error("自定义异常 :", l);
            throw l;
        } catch (Exception e) {
            logger.error("商品列表 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "productList");
        }

        return resultMap;
    }

    @Override
    public Integer chaxun(int id, int storeId) throws LaiKeAPIException {
        try {
            //获取当前商品信息
            ProductListModel productListModel = new ProductListModel();
            productListModel.setStore_id(storeId);
            productListModel.setActive("all");
            productListModel.setMch_status(DictionaryConst.GoodsMchExameStatus.EXAME_PASS_STATUS.toString());
            productListModel.setId(id);
            productListModel = productListModelMapper.selectOne(productListModel);
            if (productListModel != null) {
                //判断当前商品是否上架
                if (productListModel.getStatus().equals(DictionaryConst.GoodsStatus.NEW_GROUNDING.toString())) {
                    Map<String, Object> parmaMap = new HashMap<>();
                    parmaMap.put("store_id", storeId);
                    parmaMap.put("mch_status", DictionaryConst.GoodsStatus.NEW_GROUNDING);
                    List<Integer> rstatusList = new ArrayList<>();
                    rstatusList.add(DictionaryConst.OrdersStatus.ORDERS_R_STATUS_UNPAID);
                    rstatusList.add(DictionaryConst.OrdersStatus.ORDERS_R_STATUS_CONSIGNMENT);
                    rstatusList.add(DictionaryConst.OrdersStatus.ORDERS_R_STATUS_DISPATCHED);
                    parmaMap.put("rstatusList", rstatusList);
                    List<Map<String, Object>> resultGoodsList = productListModelMapper.getProductListJoinOrderDetailsDynamic(parmaMap);
                    if (resultGoodsList != null) {
                        return 3;
                    }
                }
                return 1;
            }
        } catch (ClassCastException c) {
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSLXBZQ, "参数类型不正确", "chaxun");
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("chaxun 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "chaxun");
        }
        return null;
    }

    @Override
    public Map<String, Object> attributeName1(int storeId, String attributeName, Map<String, Object> attrValue) throws LaiKeAPIException {
        Map<String, Object> resultMap = new HashMap<>(16);
        try {
            Map<String, Object> parmaMap = new HashMap<>(16);
            List<Integer> storeIds = new ArrayList<>();
            storeIds.add(0);
            storeIds.add(storeId);
            parmaMap.put("storeIds", storeIds);
            parmaMap.put("status", 1);
            parmaMap.put("name", attributeName);
            List<Map<String, Object>> attributeAllList = skuModelMapper.getAttributeDynamic(parmaMap);
            if (attributeAllList != null && attributeAllList.size() > 0) {
                //获取当前属性被选中的属性值
                List<Map<String, Object>> attrCheckedList = new ArrayList<>();
                if (attrValue != null && attrValue.containsKey("attr_list")) {
                    attrCheckedList = DataUtils.cast(attrValue.get("attr_list"));
                }
                for (Map<String, Object> map : attributeAllList) {
                    //属性Id
                    int sid = Integer.parseInt(map.get("id").toString());
                    //属性名称
                    String mainName = map.get("name").toString();
                    //获取该类型下的属性值
                    parmaMap.clear();
                    parmaMap.put("type", DictionaryConst.SkuType.SKUTYPE_VALUE);
                    parmaMap.put("sid", sid);
                    List<Map<String, Object>> attributeValueList = skuModelMapper.getAttributeDynamic(parmaMap);
                    //处理属性值
                    for (Map<String, Object> valueMap : attributeValueList) {
                        boolean valueStatus = false;
                        String name = valueMap.get("name").toString();
                        //判断是否被选中
                        if (attrCheckedList != null) {
                            for (Map<String, Object> value : attrCheckedList) {
                                String checkName = DataUtils.cast(value.get("attr_name"));
                                if (!StringUtils.isEmpty(checkName)) {
                                    if (name.equals(checkName)) {
                                        valueStatus = true;
                                    }
                                }
                            }
                        }
                        valueMap.put("value", name);
                        valueMap.put("status", valueStatus);
                        valueMap.remove("name");
                    }
                    resultMap.put(mainName, attributeValueList);
                }
            } else {
                resultMap.put(attributeName, new ArrayList<>());
            }
        } catch (ClassCastException c) {
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSLXBZQ, "参数类型不正确", "attribute1");
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("获取属性值 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "attribute1");
        }

        return resultMap;
    }

    @Override
    public boolean addProduct(UploadMerchandiseVo vo, String adminName, int mchId, int type) throws LaiKeAPIException {
        //保存规格属性集
        List<ConfiGureModel> saveConfigGureList = new ArrayList<>();
        //是否为编辑商品
        boolean isEdit = false;
        try {
            if (vo.getActive() == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_QXZZCHDLX, "请选择支持活动类型");
            }

            ProductListModel productListOld = null;
            if (vo.getpId() != null) {
                isEdit = true;
                productListOld = productListModelMapper.selectByPrimaryKey(vo.getpId());
            }

            ProductListModel productListModel = new ProductListModel();
            //判断商品名称是否重复
            if (productListOld == null || !productListOld.getProduct_title().equals(vo.getProductTitle())) {
                productListModel.setStore_id(vo.getStoreId());
                productListModel.setProduct_title(vo.getProductTitle());
                productListModel.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS.toString());
                productListModel.setMch_id(mchId);
                if (productListModelMapper.selectCount(productListModel) > 0) {
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_NYCZGSPQWZFTJ, "您有存在该商品,请勿重复添加");
                }
            }
            productListModel = new ProductListModel();
            if (isEdit) {
                productListModel.setId(vo.getpId());
            }
            productListModel.setStore_id(vo.getStoreId());
            productListModel.setProduct_title(vo.getProductTitle());
            productListModel.setSubtitle(vo.getSubtitle());
            productListModel.setScan(vo.getScan());
            if (StringUtils.isEmpty(vo.getScan())) {
                productListModel.setScan("");
            }
            productListModel.setProduct_number(BuilderIDTool.getGuid());
            productListModel.setProduct_class(vo.getProductClassId());
            productListModel.setBrand_id(com.laiketui.core.diyJavaFunction.StringUtils.stringParseInt(vo.getBrandId()));
            productListModel.setKeyword(vo.getKeyword());
            productListModel.setWeight(vo.getWeight());
            productListModel.setImgurl(vo.getShowImg());
            productListModel.setContent(vo.getContent());
            productListModel.setRichList(StringUtils.isNotEmpty(vo.getRichList()) ? vo.getRichList() : "");
            productListModel.setMin_inventory(vo.getStockWarn());
            //序号
            productListModel.setSort(vo.getSort());
            //处理商品类型
            List<Object> typeList = new ArrayList<>();
            if (StringUtils.isNotEmpty(vo.getsType())) {
                typeList = new ArrayList<>(Arrays.asList(vo.getsType().split(SplitUtils.DH)));
            }
            productListModel.setS_type(StringUtils.stringImplode(typeList, SplitUtils.DH, true));

            productListModel.setShow_adr(vo.getDisplayPosition());
            productListModel.setDistributor_id(vo.getDistributorId());
            productListModel.setFreight(vo.getFreightId() + "");
            productListModel.setActive(vo.getActive() + "");

            //后台能修改所有店铺商品
            if (!isEdit || GloabConst.LktConfig.LKT_CONFIG_TYPE_PT != type) {
                productListModel.setMch_id(mchId);
            }
            productListModel.setMch_status(vo.getMchStatus() + "");
            productListModel.setInitial(vo.getInitial());
            productListModel.setWeight_unit(vo.getUnit());
            if (productListOld != null){
                productListModel.setVolume(productListOld.getVolume());
            }else {
                productListModel.setVolume(vo.getVolume());
            }
            if (StringUtils.isEmpty(vo.getCoverMap())) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_QSCFMT, "请上传封面图");
            }
            productListModel.setCover_map(ImgUploadUtils.getUrlImgByName(vo.getCoverMap(), true));
            //是否为分销商品
            int isDistribution = 0;

            // 活动类型处理+展示位置数据处理
            List<String> showAdrs = new ArrayList<>();
            if (StringUtils.isNotEmpty(productListModel.getShow_adr())) {
                showAdrs = new ArrayList<>(Arrays.asList(productListModel.getShow_adr().split(SplitUtils.DH)));
            }
            //展示位
            showAdrs.add(DictionaryConst.GoodsShowAdr.GOODSSHOWADR_DEFAULT.toString());
            if (productListModel.getActive().equals(DictionaryConst.GoodsActive.GOODSACTIVE_POSITIVE_PRICE.toString())) {
                String showAdrStr = productListModel.getShow_adr();
                if (!StringUtils.isEmpty(showAdrStr)) {
                    showAdrs = Arrays.asList(showAdrStr.split(SplitUtils.DH));
                }
            } else if (productListModel.getActive().equals(DictionaryConst.GoodsActive.GOODSACTIVE_VIP_DISCOUNT.toString())) {
                isDistribution = 1;
            }

            //消息类型 1.订单(待发货) 2.订单(售后) 3.订单(提醒发货) 4.订单(订单关闭) 5.订单(新订单) 6.订单(收货) 7.商品(审核) 8.商品(下架) 9.商品(补货) 10.商品(新商品上架) 11.商品(分类) 12.商品(品牌)
            Integer loggerGoodsType = null;
            StringBuilder loggerGoodsText = new StringBuilder(SplitUtils.DH);
            if (vo.getMchStatus() == null) {
                // 默认暂不审核
                vo.setMchStatus(DictionaryConst.GoodsMchExameStatus.EXAME_WAIT_STATUS);
            }
            productListModel.setMch_status(vo.getMchStatus().toString());
            //非平台则需要审核
            if (GloabConst.LktConfig.LKT_CONFIG_TYPE_PT == type) {
                //平台发布商品直接审核通过
                loggerGoodsType = 10;
                loggerGoodsText.append("新商品上架");
                productListModel.setIs_zixuan("1");
                productListModel.setMch_status(DictionaryConst.GoodsMchExameStatus.EXAME_PASS_STATUS.toString());
            } else if (GloabConst.LktConfig.LKT_CONFIG_TYPE_MCH == type || GloabConst.LktConfig.LKT_CONFIG_TYPE_PC == type) {
                if (GloabConst.LktConfig.LKT_CONFIG_TYPE_PC == type) {
                    loggerGoodsType = 7;
                    loggerGoodsText.append("审核");
                    vo.setMchStatus(DictionaryConst.GoodsMchExameStatus.EXAME_WAIT_STATUS);
                }
                //判断是否是自营店铺,自营店铺直接跳过审核
                if (customerModelMapper.getStoreMchId(vo.getStoreId()).equals(mchId)) {
                    logger.debug("店铺id{}为自营店铺,发布商品自动跳过审核", mchId);
                    productListModel.setMch_status(DictionaryConst.GoodsMchExameStatus.EXAME_PASS_STATUS.toString());
                    productListModel.setIs_zixuan("1");
                    loggerGoodsType = 10;
                }
            }

            //商品展示位置数据处理
            List<Object> adrList = new ArrayList<>(showAdrs);
            productListModel.setShow_adr(StringUtils.stringImplode(adrList, SplitUtils.DH, true));

            productListModel.setIs_distribution(isDistribution);
            productListModel.setPublisher(adminName);
            //视频文件
            if (vo.getVideo() != null && !vo.getVideo().isEmpty()) {
                productListModel.setVideo(ImgUploadUtils.getUrlImgByName(vo.getVideo(), true));
            }else {
                productListModel.setVideo("");
            }
            if (vo.getProVideo() != null && !vo.getProVideo().isEmpty()) {
                productListModel.setProVideo(ImgUploadUtils.getUrlImgByName(vo.getProVideo(), true));
            }else {
                productListModel.setProVideo("");
            }
            //校验商品数据
            productListModel = DataCheckTool.checkGoodsDataFormate(productListModel);

            //递归找上级
            String classIdStr = strOption(vo.getStoreId(), Integer.parseInt(productListModel.getProduct_class()), "");
            if (StringUtils.isEmpty(classIdStr)) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SPLBBCZ, "商品类别id不存在");
            }
            productListModel.setProduct_class(classIdStr);
            BrandClassModel brandClassModel = new BrandClassModel();
            brandClassModel.setBrand_id(productListModel.getBrand_id());
            brandClassModel = brandClassModelMapper.selectOne(brandClassModel);
            if (brandClassModel == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_PPBCZ, "品牌不存在");
            }
            String classTop = classIdStr.split("-")[1];
            if (!brandClassModel.getCategories().contains(String.format(",%s,", classTop))) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SPLBBZGPPX, "商品类别不在该品牌下");
            }

            //商品展示图处理
            List<String> goodsImgUrlList = new ArrayList<>();
            String goodsImgUrl = productListModel.getImgurl();
            goodsImgUrlList = Arrays.asList(goodsImgUrl.split(","));

            //处理属性 [{"cbj":"1","yj":"12","sj":"11","kucun":"111","attr_list":[{"attr_id":"","attr_name":"蓝色","attr_group_name":"颜色"}]}]
            String attrStr = vo.getAttrArr();
            if (StringUtils.isEmpty(attrStr)) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_QTXSX, "请填写属性", "addProduct");
            }
            //编码处理
            attrStr = URLDecoder.decode(attrStr, GloabConst.Chartset.UTF_8);
            //当前无需回收的属性 id
            List<Integer> notRecycleAttrIds = new ArrayList<>();
            //条形码
            String attrBraCode = null;
            //入库编码
            String attrName = null;
            logger.debug("新上传的商品规格参数 :{}", attrStr);
            List<Map<String, Object>> attrAllList = JSON.parseObject(attrStr, new TypeReference<List<Map<String, Object>>>() {
            });
            int stockNum = 0;

            //当前所有属性的图片
            Map<String, Object> attrValueImageMap = new HashMap<>(16);
            for (Map<String, Object> map : attrAllList) {
                if(StringUtils.isEmpty(map.get("cbj")) || StringUtils.isEmpty(map.get("sj")) || StringUtils.isEmpty(map.get("yj")) || StringUtils.isEmpty(map.get("img"))){
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERRCODE_QWSSX, "请完善属性");
                }
                //成本价
                BigDecimal cbj = new BigDecimal(map.get("cbj").toString());
                BigDecimal price = new BigDecimal(map.get("sj").toString());
                BigDecimal yprice = new BigDecimal(map.get("yj").toString());
                //条形码
                attrBraCode = MapUtils.getString(map, "bar_code");
                //入库编码
                attrName = MapUtils.getString(map, "name");
                Integer currentNum = 0;
                //当前规格库存
                if (!Objects.isNull(vo.getSellType())) {
                    //TODO 预售商品定金模式库存设置为最大
                    if (vo.getSellType() == 1) {
                        currentNum = 9999999;
                    } else if (vo.getSellType() == 2) {
                        currentNum = vo.getSellNum();
                    }
                } else {
                    if (!map.containsKey("kucun")) {
                        logger.debug("保存商品的时候库存为空>>>{}", JSON.toJSONString(map));
                        throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_KCBNWK, "库存不能为空");
                    }
                    currentNum = MapUtils.getInteger(map, "kucun");
                    if (currentNum == null || currentNum == 0) {
                        logger.debug("保存商品的时候库存为空>>>{}", JSON.toJSONString(map));
                        throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_KCBNWK, "库存不能为空");
                    }
                }
                stockNum += currentNum;
                List<Map<String, Object>> attrList = DataUtils.cast(map.get("attr_list"));
                if (attrList == null) {
                    attrList = new ArrayList<>();
                }
                //用于保存属性的集合 (颜色分类_LKT_144=花色_LKT_145...)
                Map<String, Object> attrbiuteStrMap = new HashMap<>(16);
                ConfiGureModel saveConfiGureModel = new ConfiGureModel();
                for (Map<String, Object> attrMap : attrList) {
                    //当前规格key和value
                    String attributeGroupName = attrMap.get("attr_group_name").toString();
                    String attributeName = attrMap.get("attr_name").toString();
                    logger.debug("正在处理规格：{}{}", attributeGroupName, attributeName);
                    //记录规格
                    int attrNameId;
                    SkuModel saveSkuModel = new SkuModel();
                    saveSkuModel.setStore_id(vo.getStoreId());
                    saveSkuModel.setAdmin_name(adminName);
                    saveSkuModel.setType(DictionaryConst.SkuType.SKUTYPE_NAME);
                    saveSkuModel.setStatus(SkuModel.SKU_STATUS_INVALID);
                    //店铺发布、编辑商品时,设置的新属性,保存在SKU中,只要是商品未审核通过,则应不生效,只有审核通过或者平台手动开启方可生效
                    if (productListModel.getMch_status().equals(DictionaryConst.GoodsMchExameStatus.EXAME_PASS_STATUS.toString())) {
                        saveSkuModel.setStatus(SkuModel.SKU_STATUS_TAKE_EFFECT);
                    }
                    saveSkuModel.setRecycle(0);
                    //根据属性名称获取属性id
                    int attrGourpNameId = getAttributeId(vo.getStoreId(), DictionaryConst.SkuType.SKUTYPE_NAME, attributeGroupName, 0);
                    if (attrGourpNameId > 0) {
                        // 当属性名称ID不为0，SKU表里有数据
                        attrNameId = getAttributeId(vo.getStoreId(), DictionaryConst.SkuType.SKUTYPE_VALUE, attributeName, attrGourpNameId);
                        if (attrNameId == 0) {
                            //没找到则添加属性
                            String code = this.getSkuCode(vo.getStoreId(), null, attrGourpNameId);
                            saveSkuModel.setCode(code);
                            saveSkuModel.setType(DictionaryConst.SkuType.SKUTYPE_VALUE);
                            saveSkuModel.setName(attributeName);
                            saveSkuModel.setAdd_date(new Date());
                            saveSkuModel.setSid(attrGourpNameId);
                           if (!StringUtils.isEmpty(vo.getSellType())){
                                saveSkuModel.setIs_examine(1);
                                saveSkuModel.setStatus(SkuModel.SKU_STATUS_TAKE_EFFECT);
                            }
                            skuModelMapper.insertSelective(saveSkuModel);
                            attrNameId = saveSkuModel.getId();
                        }
                    } else {
                        //全部都没有则全部添加
                        String code = this.getSkuCode(vo.getStoreId(), attributeGroupName, null);
                        saveSkuModel.setCode(code);
                        saveSkuModel.setName(attributeGroupName);
                        saveSkuModel.setType(DictionaryConst.SkuType.SKUTYPE_NAME);
                        saveSkuModel.setSid(0);
                        if (!StringUtils.isEmpty(vo.getSellType())){
                            saveSkuModel.setIs_examine(1);
                            saveSkuModel.setStatus(SkuModel.SKU_STATUS_TAKE_EFFECT);
                        }
                        saveSkuModel.setAdd_date(new Date());
                        skuModelMapper.insertSelective(saveSkuModel);
                        attrGourpNameId = saveSkuModel.getId();
                        code = this.getSkuCode(vo.getStoreId(), null, saveSkuModel.getId());
                        saveSkuModel.setId(null);
                        saveSkuModel.setSid(attrGourpNameId);
                        saveSkuModel.setCode(code);
                        saveSkuModel.setName(attributeName);
                        saveSkuModel.setType(DictionaryConst.SkuType.SKUTYPE_VALUE);
                        saveSkuModel.setAdd_date(new Date());
                        if (!StringUtils.isEmpty(vo.getSellType())){
                            saveSkuModel.setIs_examine(1);
                            saveSkuModel.setStatus(SkuModel.SKU_STATUS_TAKE_EFFECT);
                        }
                        skuModelMapper.insertSelective(saveSkuModel);
                        attrNameId = saveSkuModel.getId();
                    }

                    //拼接属性 规格+str+id
                    attributeGroupName += "_LKT_" + attrGourpNameId;
                    attributeName += "_LKT_" + attrNameId;
                    attrMap.put("attr_group_name", attributeGroupName);
                    attrMap.put("attr_name", attributeName);
                    //属性序列化处理
                    attrbiuteStrMap.put(attributeGroupName, attributeName);
                }
                saveConfiGureModel.setCostprice(cbj);
                saveConfiGureModel.setPrice(price);
                saveConfiGureModel.setYprice(yprice);
                saveConfiGureModel.setNum(currentNum);
                saveConfiGureModel.setTotal_num(currentNum);
                saveConfiGureModel.setBar_code(attrBraCode);
                saveConfiGureModel.setName(attrName);

                //序列化
                saveConfiGureModel.setAttribute(SerializePhpUtils.JavaSerializeByPhp(attrbiuteStrMap));
                //当前属性图片
                attrValueImageMap.put(saveConfiGureModel.getAttribute(), ImgUploadUtils.getUrlImgByName(map.get("img") + "", true));
                //如果是修改
                if (isEdit) {
                    //获取之前商品属性信息 看当前属性是否是之前的属性,如果是之前的属性则做修改 否则做添加
                    ConfiGureModel confiGureOld = new ConfiGureModel();
                    confiGureOld.setPid(productListModel.getId());
                    confiGureOld.setAttribute(saveConfiGureModel.getAttribute());
                    confiGureOld = confiGureModelMapper.selectOne(confiGureOld);
                    if (confiGureOld != null) {
                        //标记无需回收的规格
                        notRecycleAttrIds.add(confiGureOld.getId());
                        //修改当前属性
                        ConfiGureModel confiGureNew = new ConfiGureModel();
                        confiGureNew.setId(confiGureOld.getId());
                        //规格图片处理
                        String currentImage = attrValueImageMap.get(confiGureOld.getAttribute()) + "";
                        if (!StringUtils.isEmpty(currentImage)) {
                            confiGureNew.setImg(currentImage);
                        }
                        confiGureNew.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS.toString());
                        confiGureNew.setCostprice(saveConfiGureModel.getCostprice());
                        confiGureNew.setYprice(saveConfiGureModel.getYprice());
                        confiGureNew.setPrice(saveConfiGureModel.getPrice());
                        confiGureNew.setNum(saveConfiGureModel.getNum());
                        confiGureNew.setTotal_num(saveConfiGureModel.getTotal_num());
                        if (saveConfiGureModel.getName() != null ){
                            confiGureNew.setName(saveConfiGureModel.getName());
                        }
                        if (saveConfiGureModel.getBar_code() != null){
                            confiGureNew.setBar_code(saveConfiGureModel.getBar_code());
                        }
                        int count = confiGureModelMapper.updateByPrimaryKeySelective(confiGureNew);
                        if (count < 1) {
                            logger.info("修改商品规格信息失败 参数:" + JSON.toJSONString(productListModel));
                            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SXBCSB, "属性保存失败", "addProduct");
                        }
                        String content;

                        StockModel saveStockModel = new StockModel();
                        saveStockModel.setTotal_num(saveConfiGureModel.getTotal_num());
                        saveStockModel.setFlowing_num(confiGureOld.getTotal_num());
                        if (!confiGureOld.getNum().equals(saveConfiGureModel.getNum())) {
                            //清空之前的库存记录重新记录
                            StockModel stockDel = new StockModel();
                            stockDel.setStore_id(vo.getStoreId());
                            stockDel.setProduct_id(productListModel.getId());
                            stockDel.setAttribute_id(confiGureOld.getId());
                            stockModelMapper.delete(stockDel);
                            //添加库存信息
                            content = adminName + "增加商品规格库存" + saveConfiGureModel.getNum();
                            saveStockModel.setId(null);
                            saveStockModel.setStore_id(vo.getStoreId());
                            saveStockModel.setProduct_id(productListModel.getId());
                            saveStockModel.setAttribute_id(saveConfiGureModel.getId());
                            saveStockModel.setTotal_num(saveConfiGureModel.getTotal_num());
                            saveStockModel.setType(StockModel.StockType.STOCKTYPE_WAREHOUSING);
                            saveStockModel.setContent(content);
                            saveStockModel.setFlowing_num(saveConfiGureModel.getNum());
                            saveStockModel.setAdd_date(new Date());
                            count = stockModelMapper.insertSelective(saveStockModel);
                            if (count < 1) {
                                logger.info("库存记录失败 参数:" + JSON.toJSONString(saveStockModel));
                                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_KCJLSB, "库存记录失败", "addProduct");
                            }
                        }
                        //如果库存有变化则清空之前的库存记录

                        // 当属性库存低于等于预警值
                        if (productListModel.getMin_inventory() >= saveConfiGureModel.getNum()) {
                            content = "预警";
                            saveStockModel.setId(null);
                            saveStockModel.setType(StockModel.StockType.AGREEMENTTYPE_WAREHOUSING_WARNING);
                            saveStockModel.setContent(content);
                            saveStockModel.setUser_id(adminName);
                            saveStockModel.setAdd_date(new Date());
                            count = stockModelMapper.insertSelective(saveStockModel);
                            if (count < 1) {
                                logger.info("库存记录失败 参数:" + JSON.toJSONString(saveStockModel));
                                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_KCYJJLSB, "库存预警记录失败", "addProduct");
                            }
                        }

                        //做了修改无需做保存,下一个
                        continue;
                    }
                }
                //需要保存的新规格
                saveConfigGureList.add(saveConfiGureModel);
            }
            //第一张为商品主图
            productListModel.setImgurl(ImgUploadUtils.getUrlImgByName(goodsImgUrlList.get(0), true));
            productListModel.setNum(stockNum);
            productListModel.setAdd_date(new Date());
            //保存商品
            int count;
            if (isEdit) {
                count = productListModelMapper.updateByPrimaryKeySelective(productListModel);
            } else {
                productListModel.setLabel("");
                //获取最新序号
                if (productListModel.getSort() == null){
                    int sort = productListModelMapper.getGoodsMaxSort(vo.getStoreId());
                    productListModel.setSort(sort);
                }
                count = productListModelMapper.insertSelective(productListModel);
                //添加跳转路径
                publicAdminService.addJumpPath(vo, productListModel.getId() + "", productListModel.getProduct_title(), JumpPathModel.JumpType.JUMP_TYPE0_GOODS, JumpPathModel.JumpType.JUMP_TYPE_APP,
                        GloabConst.LaikeTuiUrl.JumpPath.GOODS_APP, new String[]{productListModel.getId() + ""});
                publicAdminService.addJumpPath(vo, productListModel.getId() + "", productListModel.getProduct_title(), JumpPathModel.JumpType.JUMP_TYPE0_GOODS, JumpPathModel.JumpType.JUMP_TYPE_PC,
                        GloabConst.LaikeTuiUrl.JumpPath.GOODS_PC, new String[]{productListModel.getId() + ""});
            }
            if (count < 1) {
                logger.info("商品保存失败 参数:" + JSON.toJSONString(productListModel));
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SPBCSB, "商品保存失败", "addProduct");
            }
            //发布商品记录
            if (loggerGoodsType != null) {
                com.laiketui.plugin.common.domain.base.MessageLoggingModal messageLoggingSave = new com.laiketui.plugin.common.domain.base.MessageLoggingModal();
                messageLoggingSave.setStore_id(vo.getStoreId());
                messageLoggingSave.setMch_id(productListModel.getMch_id());
                messageLoggingSave.setType(loggerGoodsType);
                messageLoggingSave.setParameter(productListModel.getId() + "");
                messageLoggingSave.setContent(productListModel.getProduct_title() + loggerGoodsText);
                messageLoggingSave.setAdd_date(new Date());
                messageLoggingModalMapper.insertSelective(messageLoggingSave);
            }
            //轮播图处理
            if (isEdit) {
                //修改之前图片,把之前图片删除
                ProductImgModel delProductImgModel = new ProductImgModel();
                delProductImgModel.setProduct_id(productListModel.getId());
                count = productImgModelMapper.delete(delProductImgModel);
                logger.info("产品之前展示图删除失败 商品id:{} 删除状态:{}", productListModel.getId(), count > 0);
            }
            //添加轮播图
            for (String img : goodsImgUrlList) {
                ProductImgModel saveImg = new ProductImgModel();
                saveImg.setProduct_url(ImgUploadUtils.getUrlImgByName(img, true));
                saveImg.setProduct_id(productListModel.getId());
                saveImg.setSeller_id(adminName);
                saveImg.setAdd_date(new Date());
                count = productImgModelMapper.insertSelective(saveImg);
                if (count < 1) {
                    logger.info("产品展示图上传失败 参数:" + JSON.toJSONString(saveImg));
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CPZSTSCSB, "产品展示图上传失败", "addProduct");
                }
            }
            //规格回收处理
            if (isEdit) {
                //回收除了当前的规格其它属性
                count = confiGureModelMapper.delAppointConfiGureInfo(productListModel.getId(), notRecycleAttrIds);
                if (count < 1) {
                    logger.debug("未回收规格,可能是在原来的基础上做了新增/修改;规格Id集:{}", notRecycleAttrIds);
                }
            }

            //添加库存、规格信息
            for (ConfiGureModel confiGureModel : saveConfigGureList) {
                //规格图片处理
                String currentImage = ImgUploadUtils.getUrlImgByName(attrValueImageMap.get(confiGureModel.getAttribute()) + "", false);
                //验证属性图片是否上传
                if (StringUtils.isEmpty(currentImage)) {
                    //没有图片则查看当前属性是否存在过，如果存在过则取之前图片
                    ConfiGureModel confiGureOld = new ConfiGureModel();
                    confiGureOld.setPid(productListModel.getId());
                    confiGureOld.setAttribute(confiGureModel.getAttribute());
                    confiGureOld = confiGureModelMapper.selectOne(confiGureOld);
                    if (confiGureOld == null) {
                        logger.debug("属性id【{}】没有上传图片", confiGureModel.getId());
                        throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_QSCSXTP, "请上传属性图片");
                    } else {
                        //属性老图
                        currentImage = confiGureOld.getImg();
                        logger.debug("商品id{},属性字符串{},未上传新属性图片,延续用之前的图片", productListModel.getId(), confiGureModel.getAttribute());
                    }
                }
//                JSONObject jsonObject = JSONObject.parseObject(confiGureModel.getAttribute());
//                String bar_code = jsonObject.get("bar_code").toString();
//                String name = jsonObject.get("name").toString();
                confiGureModel.setImg(currentImage);

                //库存数量
                int attrNum = confiGureModel.getNum();
                confiGureModel.setNum(0);
                confiGureModel.setTotal_num(0);
                confiGureModel.setPid(productListModel.getId());
                confiGureModel.setMin_inventory(productListModel.getMin_inventory());
                confiGureModel.setBar_code(attrBraCode);
                confiGureModel.setName(attrName);
                confiGureModel.setColor("");
                confiGureModel.setCtime(new Date());
                confiGureModel.setUnit(productListModel.getWeight_unit());
                count = confiGureModelMapper.insertSelective(confiGureModel);
                if (count < 1) {
                    logger.info("库存记录添加失败 参数:" + JSON.toJSONString(confiGureModel));
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_KCJLTJSB, "库存记录添加失败", "addProduct");
                }
                //添加库存信息
                AddStockVo addStockVo = new AddStockVo();
                addStockVo.setStoreId(vo.getStoreId());
                addStockVo.setId(confiGureModel.getId());
                addStockVo.setPid(confiGureModel.getPid());
                addStockVo.setAddNum(attrNum);
                publicStockService.addGoodsStock(addStockVo, adminName);
            }
            vo.setpId(productListModel.getId());
            return true;
        } catch (ClassCastException c) {
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSLXBZQ, "参数类型不正确", "addProduct");
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("保存商品 异常:", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SPSCSB_001, "商品上传失败", "addProduct");
        }
    }

    @Override
    public String getSkuCode(int storeId, String skuName, Integer sid) throws LaiKeAPIException {
        String code = "LKT_%s_%s";
        try {
            if (sid == null || sid == 0) {
                //生成父类属性code规则
                String skuNamePY = PinyinUtils.getPinYinHeadChar(skuName).toUpperCase();
                String codeTemp = skuNamePY;
                int count;
                int index = 0;
                do {
                    codeTemp = String.format(code, codeTemp, storeId);
                    SkuModel sku = new SkuModel();
                    sku.setSid(0);
                    sku.setCode(codeTemp);
                    count = skuModelMapper.selectCount(sku);
                    if (count > 0) {
                        //父类重名+1
                        index++;
                        codeTemp = skuNamePY + index;
                    }
                } while (count > 0);
                code = codeTemp;
            } else {
                int index = 0;
                //获取属性上级code LKT_YS_0
                SkuModel sku = new SkuModel();
                sku.setId(sid);
                sku.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS);
                sku = skuModelMapper.selectOne(sku);
                if (sku == null) {
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SJBCZ, "上级不存在");
                }
                String thisCode = sku.getCode();
                //获取之前最新子级规则然后+1
                String code1 = skuModelMapper.getAttributeByCode(sid);
                String str = "_";
                if (!StringUtils.isEmpty(code1)) {
                    //属性子级拼接规则 LKT_name_storeId_子级名称_001(++)...
                    String codeTemp = code1.substring(code1.lastIndexOf("_"));
                    if (codeTemp.indexOf("R") > 0) {
                        str += "R";
                    }
                    index = Integer.parseInt(code1.substring(code1.lastIndexOf(str) + str.length()));
                }
                //生成子属性code规则
                code = thisCode + str + String.format("%03d", index + 1);
            }
            //判断code是否重复重复则拼接数字
            SkuModel skuCount = new SkuModel();
            skuCount.setStore_id(storeId);
            skuCount.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS);
            int count;
            int num = 0;
            String codeTemp = code;
            do {
                skuCount.setCode(codeTemp);
                count = skuModelMapper.selectCount(skuCount);
                //已存在的sku则继续拼接,直到名称不相同
                logger.debug("已存在的sku则继续拼接,直到名称不相同 {}", codeTemp);
                num++;
                //重名规则'R'
                codeTemp = code + String.format("_R%s", num);
            } while (count > 0);
            if (num > 1) {
                code = codeTemp;
            }
        } catch (Exception e) {
            logger.error("获取属性代码 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "getSkuCode");
        }
        return code;
    }

    @Override
    public boolean delGoodsById(int storeId, int goodsId, Integer mchId) throws LaiKeAPIException {
        try {
            ProductListModel productListModel = productListModelMapper.selectByPrimaryKey(goodsId);
            if (productListModel == null) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SPBCZ, "商品不存在");
            }
            if (mchId != null) {
                if (!productListModel.getMch_id().equals(mchId)) {
                    logger.info("商品回收失败 商品属于店铺{},传入的店铺id{}", productListModel.getMch_id(), mchId);
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_FFRQ, "非法入侵");
                }
            }
            ProductListModel delGoods = new ProductListModel();
            delGoods.setId(goodsId);
            delGoods.setRecycle(DictionaryConst.ProductRecycle.RECOVERY.toString());
            //回收商品
            int count = productListModelMapper.updateByPrimaryKeySelective(delGoods);
            if (count < 1) {
                logger.info("商品回收失败 参数:{}", JSON.toJSONString(delGoods));
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SPSCSB, "商品删除失败", "cancellationShop");
            }
            //回收商品属性
            count = confiGureModelMapper.delConfiGureInfo(goodsId);
            if (count < 1) {
                logger.info("商品属性回收失败,可能属性已经回收 商品id:{}", goodsId);
            }
            //删除商品图片列表
            ProductImgModel productImgModel = new ProductImgModel();
            productImgModel.setProduct_id(goodsId);
            count = productImgModelMapper.delete(productImgModel);
            if (count < 1) {
                logger.info("商品图片删除数量:{} 参数:{}", count, JSON.toJSONString(productImgModel));
            }
            //删除商品购物车数据
            CartModel cartModel = new CartModel();
            cartModel.setStore_id(storeId);
            cartModel.setGoods_id(goodsId);
            count = cartModelMapper.delete(cartModel);
            if (count < 1) {
                logger.info("购物车删除数量:{} 参数:{}", count, JSON.toJSONString(cartModel));
            }
            //删除足迹
            UserFootprintModel userFootprintModel = new UserFootprintModel();
            userFootprintModel.setStore_id(storeId);
            userFootprintModel.setP_id(goodsId);
            count = userFootprintModelMapper.delete(userFootprintModel);
            if (count < 1) {
                logger.info("足迹删除数量:{} 参数:{}", count, JSON.toJSONString(userFootprintModel));
            }
            //删除收藏
            count = userCollectionModelMapper.delGoodsOrMchCollection(storeId, goodsId, mchId);
            if (count < 1) {
                logger.info("收藏删除数量:{} 参数:{}", count, JSON.toJSONString(cartModel));
            }
            return true;
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("删除商品 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "delGoodsById");
        }
    }

    @Override
    public void upperAndLowerShelves(int storeId, String goodsIds, Integer mchId, Integer status) throws LaiKeAPIException {
        try {
            if (StringUtils.isEmpty(goodsIds)) {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSCW, "参数错误");
            }
            String[] goodsIdList = goodsIds.split(",");
            for (String goodsId : goodsIdList) {
                //获取商品信息
                ProductListModel productListModel = new ProductListModel();
                productListModel.setId(Integer.parseInt(goodsId));
                productListModel = productListModelMapper.selectOne(productListModel);
                if (productListModel == null) {
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SPBCZ, "商品不存在", "upperAndLowerShelves");
                }
                ProductListModel updateGoods = new ProductListModel();
                updateGoods.setId(productListModel.getId());
                if (status == 0) {
                    //下架 删除轮播图
                    delBanner(storeId, goodsIds, "productId");
                    updateGoods.setStatus(DictionaryConst.GoodsStatus.OFFLINE_GROUNDING.toString());
                    //上架时间置空
                    productListModelMapper.setUpperTimeNull(Integer.parseInt(goodsId));
                } else {
                    //商品上架 验证商品数据完整性
                    try {
                        productListModel.setInitial(GloabConst.ManaValue.MANA_VALUE_PICK);
                        DataCheckTool.checkGoodsDataFormate(productListModel);
                        //验证商品是否有属性,没有属性则不能上架
                        ConfiGureModel confiGureModel = new ConfiGureModel();
                        confiGureModel.setPid(updateGoods.getId());
                        confiGureModel.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS.toString());
                        int count = confiGureModelMapper.selectCount(confiGureModel);
                        if (count < 1) {
                            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SPMYSX, "商品没有属性");
                        }
                    } catch (LaiKeAPIException l) {
                        logger.info("商品{}上架失败 原因:{}", updateGoods.getId(), l.getMessage());
                        throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_QXQWSSPXX, "请先去完善商品信息", "upperAndLowerShelves");
                    }
                    if (productListModel.getNum() < 0) {
                        throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_SJSBKCBZ, "上架失败:库存不足", "upperAndLowerShelves");
                    }
                    //标记商品上架
                    updateGoods.setStatus(DictionaryConst.GoodsStatus.NEW_GROUNDING.toString());
                    //上架时间
                    updateGoods.setUpper_shelf_time(new Date());
                }
                int count = productListModelMapper.updateByPrimaryKeySelective(updateGoods);
                if (count < 1) {
                    throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CZSB, "操作失败", "upperAndLowerShelves");
                }
            }
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("上下架商品 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "upperAndLowerShelves");
        }
    }


    @Override
    public boolean editProduct(UploadMerchandiseVo vo, String adminName, int mchId, int type) throws LaiKeAPIException {
        try {
            if (vo.getpId() != null && vo.getpId() > 0) {
                return addProduct(vo, adminName, mchId, type);
            } else {
                throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSBWZ, "参数不完整", "editProduct");
            }
        } catch (ClassCastException c) {
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSLXBZQ, "参数类型不正确", "editProduct");
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("重新编辑商品 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "editProduct");
        }
    }


    /**
     * 获取属性id
     *
     * @param storeId -
     * @param type    -
     * @param name    -
     * @param sid     -
     * @return int
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2020/11/17 19:18
     */
    private int getAttributeId(int storeId, int type, String name, int sid) throws LaiKeAPIException {
        try {
            //参数列表
            Map<String, Object> parmaMap = new HashMap<>();
            List<Integer> storeIds = new ArrayList<>();
            storeIds.add(0);
            storeIds.add(storeId);
            parmaMap.put("storeIds", storeIds);
            parmaMap.put("type", type);
            parmaMap.put("name", name);
            parmaMap.put("sid", sid);
            List<Map<String, Object>> attributeValueList = skuModelMapper.getAttributeDynamic(parmaMap);
            for (Map<String, Object> map : attributeValueList) {
                return Integer.parseInt(map.get("id").toString());
            }
        } catch (ClassCastException c) {
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSLXBZQ, "参数类型不正确", "getAttributeId");
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("获取属性id 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "getAttributeId");
        }
        return 0;
    }

    @Override
    public String strOption(int storeId, int cid, String res) throws LaiKeAPIException {
        try {
            logger.info("cid={} res={} 递归找上级", cid, res);
            //获取商品类别
            ProductClassModel productClassModel = new ProductClassModel();
            productClassModel.setCid(cid);
            productClassModel.setStore_id(storeId);
            productClassModel.setRecycle(DictionaryConst.ProductRecycle.NOT_STATUS);
            productClassModel = productClassModelMapper.selectOne(productClassModel);
            if (productClassModel != null) {
                int sid = productClassModel.getSid();
                res = cid + "-" + res;
                if (sid == 0) {
                    res = "-" + res;
                } else {
                    res = this.strOption(storeId, sid, res);
                }
            }
            return res;
        } catch (ClassCastException c) {
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_CSLXBZQ, "参数类型不正确", "strOption");
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("递归商品类别 异常" + e.getMessage());
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "strOption");
        }
    }

    @Override
    public boolean delBanner(int storeId, String goodsIds, String str) throws LaiKeAPIException {
        try {
            //获取跳转url参数中包含的轮播图
            List<BannerModel> bannerModelList = bannerModelMapper.getBannersByUrl(storeId, str, goodsIds);
            for (BannerModel bannerModel : bannerModelList) {
                int count = bannerModelMapper.clearBannerById(storeId, bannerModel.getId());
                logger.info("删除轮播图id{} 执行结果:", count > 0);
            }
            return true;
        } catch (LaiKeAPIException l) {
            throw l;
        } catch (Exception e) {
            logger.error("删除轮播图 异常", e);
            throw new LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "delBanner");
        }
    }

    @Override
    public BigDecimal getGoodsFreight(User loginUser, int goodsId) throws LaiKeAPIException {
        BigDecimal freight = BigDecimal.ZERO;
        try {
            if (loginUser == null) {
                return freight;
            }
            UserAddress userAddress = new UserAddress();
            userAddress.setUid(loginUser.getUser_id());
            userAddress.setIs_default(1);
            userAddress = userAddressMapper.selectOne(userAddress);
            if (userAddress != null) {
                Map<String, Object> parmaMap = new HashMap<>(16);
                parmaMap.put("p_id", goodsId);
                List<Map<String, Object>> freightResList = productListModelMapper.getProductListJoinFreightDynamic(parmaMap);
                for (Map<String, Object> freightMap : freightResList) {
                    //商品详情运费就直接取默认运费规则
                    String defaultFreight = MapUtils.getString(freightMap, "default_freight", "");
                    if (StringUtils.isNotEmpty(defaultFreight)) {
                        DefaultFreightVO defaultFreightVO = JSON.parseObject(defaultFreight, DefaultFreightVO.class);
                        freight = new BigDecimal(defaultFreightVO.getNum2());
                    }
                }
            }
        } catch (LaiKeApiWarnException l) {
            throw l;
        } catch (Exception e) {
            logger.error("获取商品运费 异常", e);
            throw new com.laiketui.core.exception.LaiKeAPIException(ErrorCode.BizErrorCode.ERROR_CODE_WLYC, "网络异常", "getGoodsFreight");
        }
        return freight;
    }

    @Autowired
    private ProductNumberModelMapper productNumberModelMapper;

    @Autowired
    private FreightModelMapper freightModelMapper;

    @Autowired
    private DictionaryListModelMapper dictionaryListModelMapper;

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private ProductClassModelMapper productClassModelMapper;

    @Autowired
    private BrandClassModelMapper brandClassModelMapper;

    @Autowired
    private SkuModelMapper skuModelMapper;

    @Autowired
    private ProductListModelMapper productListModelMapper;

    @Autowired
    private ProductListModelExtendMapper productListModelExtendMapper;

    @Autowired
    private ConfiGureModelMapper confiGureModelMapper;

    @Autowired
    private PubliceService publiceService;

    @Autowired
    private ProductImgModelMapper productImgModelMapper;

    @Autowired
    private DistributionGradeModelMapper distributionGradeModelMapper;

    @Autowired
    private StockModelMapper stockModelMapper;

    @Autowired
    private CartModelMapper cartModelMapper;

    @Autowired
    private UserFootprintModelMapper userFootprintModelMapper;

    @Autowired
    private UserCollectionModelMapper userCollectionModelMapper;


    @Autowired
    private PublicStockService publicStockService;

    @Autowired
    private PublicAdminService publicAdminService;

    @Autowired
    private PublicDictionaryService publicDictionaryService;

    @Autowired
    private MessageLoggingModalMapper messageLoggingModalMapper;

    @Autowired
    private BannerModelMapper bannerModelMapper;
}

